define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'test/itembank/index' + location.search,
                    add_url: 'test/itembank/add',
                    edit_url: 'test/itembank/edit',
                    del_url: 'test/itembank/del',
                    multi_url: 'test/itembank/multi',
                    import_url: 'test/itembank/import',
                    table: 'itembank',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'article.title', title: "文章"},
                        {field: 'title', title: __('title')},
                        // {field: 'type', title: __('Type'), searchList: {"judge":__('Type judge'),"onechoice":__('Type onechoice'),"manychoice":__('Type manychoice'),"supplyblank":__('Type supplyblank')}, formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type'), searchList: {"onechoice":__('Type onechoice'),"manychoice":__('Type manychoice')}, formatter: Table.api.formatter.normal},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            typeChange();
			$("#c-type").change(function(){
				typeChange();
			});
        },
        edit: function () {
            Controller.api.bindevent();

            typeChange();
			$("#c-type").change(function(){
				typeChange();
			});
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    function typeChange(){
		$type = $("#c-type").val();
		$("#c-judge_answer").closest(".form-group").attr("hidden","hidden");
		$("#c-onechoice_answer").closest(".form-group").attr("hidden","hidden");
		$("#c-manychoice_answer").closest(".form-group").attr("hidden","hidden");
		$("#c-"+$type+"_answer").closest(".form-group").removeAttr("hidden");
		if($type == 'judge'){
			$("#c-values").closest(".form-group").attr("hidden","hidden");
		}else{
			$("#c-values").closest(".form-group").removeAttr("hidden");
		}
    }
    
    return Controller;
});